from django.core.cache import cache
from django.shortcuts import render
from rest_framework.generics import ListAPIView
from rest_framework.response import Response
from rest_framework.views import APIView

from home.homeser import NavSerializer, BannerSerializer
from home.models import NavModel, BannerModel


# Create your views here.

class TopNav(APIView):
    def get(self, request):
        key = 'topnav'
        cacheres = cache.get(key)
        if cacheres:
            return Response(cacheres)
        top = NavModel.objects.filter(position=True, is_show=True).order_by("order")
        ser = NavSerializer(instance=top, many=True)
        cache.set(key, ser.data, 60 * 60 * 24)
        return Response(ser.data)


class FootNav(APIView):
    def get(self, request):
        key = 'footnav'
        cacheres = cache.get(key)
        if cacheres:
            return Response(cacheres)
        foot = NavModel.objects.filter(position=False, is_show=True).order_by("order")
        ser = NavSerializer(instance=foot, many=True)
        cache.set(key, ser.data, 60 * 60 * 24)
        return Response(ser.data)


class Banner(APIView):
    def get(self, request):
        banner = BannerModel.objects.filter(is_show=True).order_by("order")
        ser = BannerSerializer(instance=banner, many=True)
        return Response(ser.data)


class BannerList(ListAPIView):
    queryset = BannerModel.objects.filter(is_show=True).order_by("order")
    serializer_class = BannerSerializer
